home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / AmiTCP30b2.lha / src / util / whoami / whoami.c < prev   
C/C++ Source or Header  |  1994-02-24  |  3KB  |  114 lines

  1. RCS_ID_C="$Id: whoami.c,v 1.3 1994/02/12 04:39:41 ppessi Exp $";
  2. /*
  3.  * whoami.c ---
  4.  *
  5.  * Author: ppessi <Pekka.Pessi@hut.fi>
  6.  *
  7.  * Copyright © 1994 AmiTCP/IP Group, <amitcp-group@hut.fi>,
  8.  *            Helsinki University of Technology, Finland.
  9.  *            All rights reserved.
  10.  *
  11.  * Created      : Wed Jan 12 01:40:38 1994 ppessi
  12.  * Last modified: Sat Feb 12 03:39:20 1994 ppessi
  13.  */
  14.  
  15. #include "whoami_rev.h"
  16.  
  17. static const char version[] = VERSTAG " "
  18.   "Copyright © 1994 AmiTCP/IP Group, <amitcp-group@hut.fi>\n"
  19.   "Helsinki University of Technology, Finland.\n";
  20.  
  21. /****** utilities/whoami ***************************************************
  22.  
  23.    NAME
  24.         whoami - prints effective current user id
  25.  
  26.    VERSION
  27.         $Id: whoami.c,v 1.3 1994/02/12 04:39:41 ppessi Exp $
  28.  
  29.    TEMPLATE
  30.         whoami
  31.  
  32.    FUNCTION
  33.         Whoami prints your effective user id.  It works even if you are
  34.         su'd.
  35.  
  36.    RETURN VALUE
  37.         Whoami return WARN, if the user id has got no user name associated.
  38.  
  39.    SEE ALSO
  40.         id
  41.  
  42. *****************************************************************************
  43. *
  44. */
  45.  
  46.  
  47. #include <exec/types.h>
  48. #include <exec/ports.h>
  49. #include <exec/semaphores.h>
  50. #include <exec/memory.h>
  51. #include <exec/execbase.h>
  52.  
  53. #include <dos/dos.h>
  54. #include <dos/dosextens.h>
  55.  
  56. #ifdef __SASC
  57. #include <clib/exec_protos.h>
  58. extern struct ExecBase* SysBase;
  59. #include <pragmas/exec_sysbase_pragmas.h>
  60. #include <proto/dos.h>
  61. #include <proto/usergroup.h>
  62. #else
  63. #include <clib/exec_protos.h>
  64. #include <clib/dos_protos.h>
  65. #include <clib/usergroup_protos.h>
  66. #include <libraries/usergroup.h>
  67. #endif
  68.  
  69. #include <devices/sana2.h>
  70. #include <net/sana2errno.h>
  71. #include <string.h>
  72.  
  73. #define USERGROUPVERSION 1    /* minimum version to use */
  74.  
  75. #ifndef MAXLINELENGTH
  76. #define MAXLINELENGTH 1024
  77. #endif
  78.  
  79. LONG whoami(void)
  80. {
  81.   LONG retval = 128;
  82.   struct DosLibrary *DOSBase;
  83.   struct ExecBase *SysBase;
  84.  
  85.   SysBase = *(struct ExecBase**)4;
  86.   DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 37L);
  87.  
  88.   if (DOSBase) {
  89.     struct Library *UserGroupBase =
  90.       OpenLibrary(USERGROUPNAME, USERGROUPVERSION);
  91.  
  92.     if (UserGroupBase != NULL) {
  93.       uid_t uid = geteuid();
  94.       struct passwd *pw = getpwuid(uid);
  95.  
  96.       if (pw != NULL) {
  97.     Printf("%s\n", pw->pw_name);
  98.     retval = RETURN_OK;
  99.       } else {
  100.     Printf("whoami: no login associated with uid %lu.\n", uid);
  101.     retval = RETURN_WARN;
  102.       }
  103.  
  104.       CloseLibrary(UserGroupBase);
  105.     } else {
  106.       Printf("whoami: cannot open %s\n", USERGROUPNAME);
  107.     }
  108.  
  109.     CloseLibrary((struct Library *)DOSBase);
  110.   }
  111.  
  112.   return retval;
  113. }
  114.